<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入楼层名称查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="buildingId">
        <el-select v-model="queryParams.buildingId" placeholder="请选择楼宇" clearable>
          <el-option
            v-for="item in buildingOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item prop="unitId">
        <el-select v-model="queryParams.unitId" placeholder="请选择单元" clearable>
          <el-option
            v-for="item in nowUnitOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="handleQuery">查询</el-button>
        <el-button type="primary" size="mini" @click="handleAdd"  >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="floorsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="id" />-->
      <el-table-column label="楼层" align="center" prop="num" />
      <el-table-column label="楼层名称" align="center" prop="name" />
      <el-table-column label="楼宇名称" align="center" prop="buildingName" />
      <el-table-column label="单元名称" align="center" prop="unitName" />
      <el-table-column label="房间数" align="center" prop="countRooms" >
        <template slot-scope="scope">
          <el-button type="text" :disabled="scope.row.countRooms === 0" @click="send(scope.row.id)">{{scope.row.countRooms}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:floors:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:floors:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改floors对话框 -->
    <add-and-update
      :visible.sync="auVisible"
      :form="form" :title="title"
      :getList="getList"
      :reset="reset"
    />
  </div>
</template>

<script>
import { listFloors, getFloors, delFloors} from "@/api/system/floors";
import {buildingList, listBuilding} from "../../../api/system/building";
import {listUnit, unitList} from "../../../api/system/unit";
import addAndUpdate from "./addAndUpdate"
export default {
  name: "Floors",
  components:{addAndUpdate},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // floors表格数据
      floorsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      auVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        num: null,
        name: null,
        unitId: null,
      },
      // 表单参数
      form: {},
      buildingOptions: [
        { label: "一号楼", value: 1},
        { label: "二号楼", value: 2}
      ],
      unitOptions: [
        { label: "一单元", value: 1},
        { label: "二单元", value: 2},
      ],
    };
  },
  created() {
    this.initHandle(this.$route.params.unitId)
    this.getList();
    buildingList().then(res => {
      this.buildingOptions = res.rows
    })
    unitList().then(res => {
      this.unitOptions = res.rows
    })
  },
  watch:{
    '$route.params.unitId'(n) {
      if(n) {
        this.initHandle(n)
      }
    }
  },
  computed:{
    nowUnitOptions(){
      return this.unitOptions.filter(item => this.queryParams.buildingId === item.buildingId)
    },
  },
  methods: {
    initHandle(id) {
      this.queryParams.unitId = id
      this.getList();
    },
    send(id) {
      this.$router.push({name: 'Rooms', params: {roomId: id}})
    },
    /** 查询floors列表 */
    getList() {
      this.loading = true;
      listFloors(this.queryParams).then(response => {
        this.floorsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        num: null,
        name: null,
        unitId: null,
        createTime: null,
        updateTime: null,
        creator: null,
        delFlag: null,
        countRooms: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.auVisible = true;
      this.title = "添加floors";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFloors(id).then(response => {
        this.form = response.data;
        this.auVisible = true;
        this.title = "修改floors";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除floors编号为"' + ids + '"的数据项？').then(function() {
        return delFloors(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/floors/export', {
        ...this.queryParams
      }, `floors_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
